<?php require('pre.inc'); ?>
<h1>Statistics</h1>


<div id="stats">
<p>Statistics shows the total number of instances, classes and predicates used in the linked open aalto data service. </p>	
<p>Total number of triples: <span id="triples"><img src="load.gif" alt="loading ..."/></span></p>
<p>Total number of distinct classes: <span id="classes"><img src="load.gif" alt="loading ..."/></span></p>
<p>Total number of distinct predicates: <span id="preds"><img src="load.gif" alt="loading ..."/></span></p>
</div>
<div>
<b>Total number of instances of a certain class:</b>
<p id="pie"><img src="load.gif" alt="loading ..."/></p>
</div>
<div>
<b>Total number of subjects using the property:</b>
<p id="bar"><img src="load.gif"/></p>
</div>

<script>
var bgraph = new BGraph();	
var prefix = bgraph.getPrefixes();
google.load("visualization", "1", {packages:["corechart"]});

var q1 = prefix+"SELECT (COUNT(*) AS ?no) { ?s ?p ?o  }";
var q2 = prefix+"SELECT (COUNT(distinct ?o) AS ?no) { ?s rdf:type ?o }";
var q3 = prefix+"SELECT (Count(distinct ?p) AS ?no) { ?s ?p ?o }";
var q4 = prefix+"SELECT ?class (COUNT(?s) AS ?count) { ?s a ?class } GROUP BY ?class ORDER BY ?count";
var q5 = prefix+"SELECT ?p (COUNT(DISTINCT ?s ) AS ?Subjects ) { ?s ?p ?o } GROUP BY ?p ORDER BY desc(?Subjects)";

function ajaxQuery(q) {
	return $.ajax({url: "http://data.aalto.fi/sparql?query="+encodeURIComponent(q)+"&format=json"});
}

function plotText(queryString,elem) {
	ajaxQuery(queryString).done(function(data) {
	// data = $.parseJSON(data); // For debugging only
	if(data.results.bindings.length>0) {
		$(elem).html(data.results.bindings[0]["no"].value);
	} else $(elem).html("No results found");
	});
}

function plotPie(queryString,elem) {
	ajaxQuery(queryString).done(function(data) {
	// data = $.parseJSON(data); // For debugging only
	if(data.results.bindings.length>0) {
				
		var data = new google.visualization.DataTable(bgraph.SparqlJSON2GoogleJSON(data));
		var viz = new google.visualization.PieChart($(elem)[0]);
				
		var options = {
			is3D:true,
			height: 300,
			width:1000,
			chartArea:{left:0,top:20,width:"50%",height:"75%"}
			};
			
		viz.draw(data,options);
		
	}});
}

function plotBar(queryString,elem) {
	ajaxQuery(queryString).done(function(data) {
	// data = $.parseJSON(data); // For debugging only
	if(data.results.bindings.length>0) {
				
		var data = new google.visualization.DataTable(bgraph.SparqlJSON2GoogleJSON(data));
		var viz = new google.visualization.BarChart($(elem)[0]);
		
		var options = {
			legend: {position: 'none'},
            bar: {groupWidth: "79%"},
            width:600, height:1500,
            is3D: true,
            vAxis: {textStyle:{fontSize: 13}},
            hAxis: {logScale: true},
            chartArea:{left:150,top:0,width:"60%",height:"75%"}
            };
		
		viz.draw(data,options);
		
	}});
}

plotText(q1,"#triples");
plotText(q2,"#classes");
plotText(q3,"#preds");

google.setOnLoadCallback(function() {
plotPie(q4,"#pie");
plotBar(q5,"#bar");
});



</script>

<?php require('post.inc'); ?>

